//单向非循环不带头结点链表
//时刻考虑空链表的情况
//组略的总结：
//单向链表想前驱，定义一个变量就够了cur
//双向链表直接走到那个地方，也是定义一个变量就可以了
public class MySingleList {
    //结点
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
   //打印链表，默认从头开始打印
    public void display(){
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //从指定结点开始打印链表
    public void display(ListNode newHead){
        ListNode cur = newHead;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //计算链表的长度
        //空链表也满足
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if(cur == null){
            this.head = node;
        }else {
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    //任意位置插入法
    public void addIndex(int index,int data){
        if(index<0 || index>size()){
            System.out.println("不合法！");
            //抛异常
        }
        if(index == 0){
            //头插法
            addFirst(data);
            return;
        }
        //尾插法的这段代码也可以省略
        if(index == size()){
            //尾插法
            addLast(data);
            return;
        }
        //到这就合法了
        ListNode cur = findIndexSubOne(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }
    public ListNode findIndexSubOne(int index){
        ListNode cur = this.head;
        while(index-1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //判断链表中是否存在某个值key
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现的某个值key的结点
    //法一：用两个变量来做
    public void removeone(int key){
        if(this.head == null){
            return;
        }
        if(this.head.val == key){
            this.head = this.head.next;
            return;
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                return;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
    }
    //法二：用一个变量来做
    public void remove(int key){
        if(this.head == null){
            return;
        }
        if(this.head.val == key){
            this.head = this.head.next;
            return;
        }
        ListNode cur = findPrevOfKey(key);
        if(cur == null){
            System.out.println("没有你要找的值！");
            return;
        }
        cur.next = cur.next.next;
    }
    public ListNode findPrevOfKey(int key){
        ListNode cur = this.head;
        while(cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除链表中所有的某个值key的结点
    //其实定义一个变量就够了，不需要两个变量
    public void removeAll(int key){
        if(this.head == null){
            return;
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        if(this.head.val == key){
            this.head = this.head.next;
        }

    }
    //清空链表
    public void clear(){
        this.head = null;
    }
}
